package com.example.fresh_fruit.util;

import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

/**
 *Token的工具类
 * @create 2022-08-22 16:39
 */
public class IDUtil {
    private final static String idName = "Id";

    //拿到Token中的封装的ID

    public static int getCurrUserId(){

        Integer id = (Integer) RequestContextHolder.currentRequestAttributes().getAttribute(idName, RequestAttributes.SCOPE_REQUEST);

        return id == null? -1 : id;
    }

    //将Token中的封装的ID放到上下文中

    public static void setCurrUserId(int id){

        RequestContextHolder.currentRequestAttributes().setAttribute(idName,id, RequestAttributes.SCOPE_REQUEST);

    }
}
